/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package firma;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import model.Firma;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import validate.FirmaValidate;

/**
 *
 * @author tomasz
 */
@Controller
//@RequestMapping("/create/firma.htm")
public class CreateFirmaForm {
    
    private dao.Firma firma;
    private dao.Account account;
    
    
    @Autowired
    public void setFirma(dao.Firma firma){
        this.firma = firma;
    }
    
    @Autowired
    public void setAccount(dao.Account account){
        this.account = account;
    }
    
    @RequestMapping(value = "/create/firma.htm/{id}",method = RequestMethod.GET)
    public String EditFirma(Model model, @PathVariable Integer id) {
        
        model.Firma firma = this.firma.findFirmaId(id);
        
        model.addAttribute(firma);
        
        return "firma/create_firma_form";
        
    }
    
    
    
    @RequestMapping(value = "/create/firma.htm", method = RequestMethod.GET)
    public String CreateFirma(Model model) {
        
        Firma firma = new Firma();
        model.addAttribute(firma);
        
        
        return "firma/create_firma_form";
        
    }
    
    

    
    @RequestMapping(value = "/create/firma.htm", method= RequestMethod.POST)
    public String processSubmit(@ModelAttribute model.Firma firma, BindingResult result,HttpServletRequest request){
        
       
        new FirmaValidate().validate(firma, result);
        
        if (result.hasErrors()) {
            
		return "firma/create_firma_form";
                
	} else {
                
                model.Account account = new model.Account();
                
                Date today = Calendar.getInstance().getTime();
                firma.setDateCreate(today);  

                this.firma.addFirma(firma);
                
                return "redirect:/firma.htm";
                
	}
        
    }
    
    
}
